﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DroidLord.app.dy
{
    class DYGetUserGoods
    {

        public static String  getRedirectUrl(String userShareUrl)
        {
            String result = "";
            try
            {
                HttpWebRequest myrequest=(HttpWebRequest)WebRequest.Create(userShareUrl);
                myrequest.Method = "GET";
                myrequest.AllowAutoRedirect = true;
                using(WebResponse wr=myrequest.GetResponse())
                {
                    
                    result = wr.ResponseUri.AbsoluteUri;
                }
                myrequest.Abort();
            }catch(Exception eee)
            {

            }
            return result;
        }
        /// <summary>
        /// https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid=MS4wLjABAAAA_4IgNVquWXWa6qe-V8xEFjXWfqvvk2efEnitQnvNkU8
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="secuid"></param>
        /// <returns></returns>
        public static String getUserInfo(String userid,String secuid)
        {
            String result = "";
            try
            {
                String myurl = "https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid=" + secuid;
                HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(myurl);
                myrequest.Method = "GET";
                myrequest.AllowAutoRedirect = true;
                using (WebResponse wr = myrequest.GetResponse())
                {
                    StreamReader myStreamReader = new StreamReader(wr.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    result = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                }
                myrequest.Abort();
            }
            catch (Exception eee)
            {

            }
            return result;
        }

        /// <summary>
        /// https://aweme.snssdk.com/aweme/v1/promotion/user/promotion/list/?aid=1128&user_id=2326200557505672&sec_user_id=MS4wLjABAAAA_3NdL-E9pUk58ZefAqX0RQVsMfQtFqLNL1KpgQh6_YSO4JhKefisXYDiZiafe6d1&cursor=0&count=20&column_id=1&sort=0&goods_type=3&shop_version=1
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="secuid"></param>
        /// <returns></returns>
        public static String  getGoodList(String uid,String secuid,String cursor,String pageCount)
        {
            String url = "https://aweme.snssdk.com/aweme/v1/promotion/user/promotion/list/?aid=1128&user_id="+uid+"&sec_user_id="+secuid+"&cursor="+cursor+"&count="+pageCount+"&column_id=1&sort=0&goods_type=3&shop_version=1";
            String result = "";
            try
            {
                
                HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(url);
                myrequest.Method = "GET";
                myrequest.AllowAutoRedirect = true;
               // myrequest.UserAgent = "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36";
                using (WebResponse wr = myrequest.GetResponse())
                {
                    StreamReader myStreamReader = new StreamReader(wr.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    result = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                }
                myrequest.Abort();
            }
            catch (Exception eee)
            {

            }
            return result;
        }

        public static string GetTimeStamp(bool bflag)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            if (bflag)
                ret = Convert.ToInt64(ts.TotalSeconds).ToString();
            else
                ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return ret;
        }

        //https://aweme.snssdk.com/aweme/v1/promotion/user/promotion/list/?user_id=109779915444&sec_user_id=MS4wLjABAAAAvZZyWmijNWfXOjZnu4RxjXpFdNUooCDMKn8XtJJ22Cc&cursor=140&count=20&column_id=0&goods_type=2&shop_version=1&storage_type=0&manifest_version_code=120001&_rticket=1596096304685&app_type=normal&iid=685690430690688&channel=aweGW&device_type=ONEPLUS+A3000&language=zh&cpu_support64=true&host_abi=armeabi-v7a&resolution=1080*1920&openudid=ef753a6e96787dab&update_version_code=12009900&cdid=8e7328e6-f8ac-49f2-81b6-de6ba5dad3f4&appTheme=dark&os_api=27&mac_address=C0%3AEE%3AFB%3AD6%3A8A%3A73&dpi=480&ac=wifi&device_id=68370507519&mcc_mnc=46011&os_version=8.1.0&version_code=120000&app_name=aweme&version_name=12.0.0&device_brand=OnePlus&ssmix=a&device_platform=android&aid=1128&ts=1596096304
        public static String getGoodList2(String uid, String secuid, String cursor,string pageCount)
        {
            String url = "https://aweme.snssdk.com/aweme/v1/promotion/user/promotion/list/?aid=1128&user_id=" + uid + "&sec_user_id=" + secuid + "&cursor=" + cursor + "&count="+pageCount+"&column_id=1&sort=0&goods_type=3&shop_version=11&storage_type=0&manifest_version_code=120001&_rticket=1596096304685&app_type=normal&iid=685690430690688&channel=aweGW&device_type=ONEPLUS+A3000&language=zh&cpu_support64=true&host_abi=armeabi-v7a&resolution=1080*1920&openudid=ef753a6e96787dab&update_version_code=12009900&appTheme=dark&os_api=27&mac_address=C0%3AEE%3AFB%3AD6%3A8A%3A73&dpi=480&ac=wifi&device_id=68370507519&mcc_mnc=46011&os_version=8.1.0&version_code=120000&app_name=aweme&version_name=12.0.0&device_brand=OnePlus&ssmix=a&device_platform=android&aid=1128&ts="+GetTimeStamp(true);
            String result = "";
            try
            {

                HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(url);
                myrequest.Method = "GET";
                myrequest.AllowAutoRedirect = true;
                myrequest.UserAgent = "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; ONEPLUS A3000 Build/AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36";
                using (WebResponse wr = myrequest.GetResponse())
                {
                    StreamReader myStreamReader = new StreamReader(wr.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    result = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                }
                myrequest.Abort();
            }
            catch (Exception eee)
            {

            }
            return result;
        }

        public static String[] parseUserId(String realUrl)
        {
            String[] userinfos = new string[2] { "", "" };
            int index = realUrl.IndexOf("?");
            if (index > 0)
            {
                String subString = realUrl.Substring(0, index);
                int index2 = subString.LastIndexOf("/");
                if (index2 > 0)
                {
                    String uid = subString.Substring(index2+1);
                    userinfos[0] = uid;
                }
                String paramString = realUrl.Substring(index + 1);
                int index3 = paramString.IndexOf("sec_uid=");
                if (index3 >= 0)
                {
                    string mypstring1 = paramString.Substring(index3 + "sec_uid=".Length);
                    int index5 = mypstring1.IndexOf("&");
                    if (index5 > 0)
                    {
                        String secuid = mypstring1.Substring(0, index5);
                        userinfos[1] = secuid;
                    }
                }
            }
            return userinfos;
        }
    }
}
